package com.chujiekeji.test;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View.OnClickListener;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.chujiekeji.test.fragment.PublishFragment;

import java.util.ArrayList;
import java.util.List;
//
//import butterknife.BindView;
//import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements OnClickListener {
    //声明ViewPager
    private ViewPager mViewPager;
    //适配器
    private FragmentPagerAdapter mAdapter;
    //装载Fragment的集合
    private List<Fragment> mFragments;

    //四个Tab对应的布局
    private LinearLayout mTabIndex;
    private LinearLayout mTabFrd;
    private LinearLayout mTabPub;
    private LinearLayout mTabHelp;
    private LinearLayout mTabMy;

    //四个Tab对应的ImageButton
    private ImageButton mImgIndex;
    private ImageButton mImgFrd;
    private ImageButton mImgPub;
    private ImageButton mImgHelp;
    private ImageButton mImgMy;

//    @BindView(R.id.message)
//    TextView mTextMessage;
//    @BindView(R.id.navigation)
//    BottomNavigationView navigation;

//    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
//            = new BottomNavigationView.OnNavigationItemSelectedListener() {
//
//        @Override
//        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//            switch (item.getItemId()) {
//                case R.id.navigation_home:
//                    mTextMessage.setText(R.string.title_home);
//                    return true;
//                case R.id.navigation_dashboard:
//                    mTextMessage.setText(R.string.title_dashboard);
//                    return true;
//                case R.id.navigation_notifications:
//                    mTextMessage.setText(R.string.title_notifications);
//                    return true;
//            }
//            return false;
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
//        ButterKnife.bind(this);
//        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        initViews();//初始化控件
        initEvents();//初始化事件
        initDatas();//初始化数据
    }
    private void initDatas() {
        mFragments = new ArrayList<>();
        //将四个Fragment加入集合中
        mFragments.add(new IndexFragment());
        mFragments.add(new FrdFragment());
        mFragments.add(new HelpFragment());
        mFragments.add(new HelpFragment());
        mFragments.add(new MyFragment());

        //初始化适配器
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {//从集合中获取对应位置的Fragment
                return mFragments.get(position);
            }

            @Override
            public int getCount() {//获取集合中Fragment的总数
                return mFragments.size();
            }

        };
        //不要忘记设置ViewPager的适配器
        mViewPager.setAdapter(mAdapter);
        //设置ViewPager的切换监听
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            //页面滚动事件
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //页面选中事件
            @Override
            public void onPageSelected(int position) {
                //设置position对应的集合中的Fragment
                mViewPager.setCurrentItem(position);
                resetImgs();
                selectTab(position);
            }

            @Override
            //页面滚动状态改变事件
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initEvents() {
        //设置四个Tab的点击事件
        mTabIndex.setOnClickListener(this);
        mTabFrd.setOnClickListener(this);
        mTabPub.setOnClickListener(this);
        mTabHelp.setOnClickListener(this);
        mTabMy.setOnClickListener(this);

    }

    //初始化控件
    private void initViews() {
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        mTabIndex = (LinearLayout) findViewById(R.id.id_tab_index);
        mTabFrd = (LinearLayout) findViewById(R.id.id_tab_frd);
        mTabPub = (LinearLayout) findViewById(R.id.id_tab_pub);
        mTabHelp = (LinearLayout) findViewById(R.id.id_tab_help);
        mTabMy = (LinearLayout) findViewById(R.id.id_tab_my);

        mImgIndex = (ImageButton) findViewById(R.id.id_tab_weixin_img);
        mImgFrd = (ImageButton) findViewById(R.id.id_tab_frd_img);
        mImgPub = (ImageButton) findViewById(R.id.id_tab_address_img);
        mImgHelp = (ImageButton) findViewById(R.id.id_tab_help_img);
        mImgMy = (ImageButton) findViewById(R.id.id_tab_setting_img);
    }

    @Override
    public void onClick(View v) {
        //先将四个ImageButton置为灰色
        resetImgs();

        //根据点击的Tab切换不同的页面及设置对应的ImageButton为绿色
        switch (v.getId()) {
            case R.id.id_tab_index:
                selectTab(0);
                break;
            case R.id.id_tab_frd:
                selectTab(1);
                break;
            case R.id.id_tab_pub:
                selectTab(2);
                break;
            case R.id.id_tab_help:
                selectTab(3);
                break;
            case R.id.id_tab_my:
                selectTab(4);
                break;

        }
    }

    private void selectTab(int i) {
        //根据点击的Tab设置对应的ImageButton为绿色
        switch (i) {
            case 0:
                mImgIndex.setImageResource(R.mipmap.tab_weixin_pressed);
                break;
            case 1:
                mImgFrd.setImageResource(R.mipmap.tab_find_frd_pressed);
                break;
            case 2:
                mImgPub.setImageResource(R.mipmap.tab_address_pressed);
                break;
            case 3:
                mImgHelp.setImageResource(R.mipmap.tab_settings_pressed);
                break;
            case 4:
                mImgMy.setImageResource(R.mipmap.tab_settings_pressed);
                break;
        }
        //设置当前点击的Tab所对应的页面
        mViewPager.setCurrentItem(i);
    }

    //将四个ImageButton设置为灰色
    private void resetImgs() {
        mImgIndex.setImageResource(R.mipmap.tab_weixin_normal);
        mImgFrd.setImageResource(R.mipmap.tab_find_frd_normal);
        mImgPub.setImageResource(R.mipmap.tab_address_normal);
        mImgHelp.setImageResource(R.mipmap.tab_settings_normal);
        mImgMy.setImageResource(R.mipmap.tab_settings_normal);
    }
}
